package com.multition.demo03;

import java.util.Date;
import java.util.Random;

/**
 * 采用多例模式设计一个 序列键生成器
 *
 */
public class Client {
    private static String key1 = "key1";
    private static String key2 = "key2";

    public static void main(String[] args) {
        Date now = new Date();
        Random random = new Random(now.getTime());

        Integer num=0;

        do{
            int abs = Math.abs(random.nextInt());
            num++;
            if (abs%2==0) {
                testCase1();
            }else{
                testCase2();
            }
        }while (num<11);
    }

    // 模拟模块一需要生成序列键
    public static void testCase1(){
        new Thread(()->{
            KeyGenerator instance = KeyGenerator.getInstance(key1);
            System.out.println(Thread.currentThread().getName() +"-----"+key1+"----->"+ instance.getNextKey());
        }).start();
    }

    // 模拟模块二需要生成序列键
    public static void testCase2(){
        new Thread(()->{
            KeyGenerator instance = KeyGenerator.getInstance(key2);
            System.out.println(Thread.currentThread().getName() +"-----"+key2+"----->"+ instance.getNextKey());
        }).start();
    }
}
